//
//  Status.m
//  Weibo
//
//  Created by Anasue on 9/14/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "Status.h"
#import "MJExtension.h"
#import "Photo.h"
#import "NSDate+MJ.h"
#import "RegexKitLite.h"
#import "RegexResult.h"
#import "EmotionAttachment.h"
#import "EmotionTool.h"
#import "User.h"

@implementation Status

- (NSDictionary *)objectClassInArray
{
    return @{@"pic_urls":[Photo class]};
}


- (NSString *)created_at
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"EEE MM dd HH:mm:ss ZZZ yyyy";
    NSDate *createdDate = [dateFormatter dateFromString:_created_at];
    if (createdDate.isThisYear) {
        if (createdDate.isToday) {
            NSDateComponents *dateComp = [createdDate deltaWithNow];
            if (dateComp.hour >=1) {
                return [NSString stringWithFormat:@"%ld小时前",(long)dateComp.hour];
            }else if (dateComp.minute >=1){
                return [NSString stringWithFormat:@"%ld分钟前",(long)dateComp.minute];
            }else{
                return @"刚刚";
            }
        }else if (createdDate.isYesterday){
            [dateFormatter setDateFormat:@"昨天 HH:mm"];
            return [dateFormatter stringFromDate:createdDate];
        
        }else{//昨天以前
            [dateFormatter setDateFormat:@"MM-dd"];
            return [dateFormatter stringFromDate:createdDate];
        }
    }else{//今年以前
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        return [dateFormatter stringFromDate:createdDate];
    }
}

- (void)setSource:(NSString *)source
{
//    Log(@"%@",source);
    if (source.length != 0) {
        NSRange range = NSMakeRange(0, 0);
        range.location = [source rangeOfString:@">"].location + 1;
        //    Log(@"%ld",range.location);
        range.length = [source rangeOfString:@"</"].location - range.location;
        //    Log(@"%ld",range.length);
        NSString *subSource = [source substringWithRange:range];
        _source = [NSString stringWithFormat:@"来自%@",subSource];
//        Log(@"%@",subSource);
    }
}

- (NSArray *)regexResultsWithText:(NSString *)text
{
    //创建文本数组
    NSMutableArray *regexResults = [NSMutableArray array];
    //设置[]的正则匹配方、式
    NSString *emotionRegex = @"\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]";
    //遍历出表情
    [text enumerateStringsMatchedByRegex:emotionRegex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
        RegexResult *regexResult = [[RegexResult alloc]init];
        regexResult.string = *capturedStrings;
        regexResult.range = *capturedRanges;
        regexResult.emotion = YES;
        [regexResults addObject:regexResult];
    }];
    //遍历出非表情
    [text enumerateStringsSeparatedByRegex:emotionRegex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
        RegexResult *regexResult = [[RegexResult alloc]init];
        regexResult.string = *capturedStrings;
        regexResult.range = *capturedRanges;
        regexResult.emotion = NO;
        [regexResults addObject:regexResult];
    }];
    //比较字段位置
    [regexResults sortUsingComparator:^NSComparisonResult(RegexResult *regexResult1, RegexResult *regexResult2) {
        NSInteger loc1 = regexResult1.range.location;
        NSInteger loc2 = regexResult2.range.location;
        return [@(loc1) compare:@(loc2)];
    }];
    return regexResults;
}

- (void)setText:(NSString *)text
{
    _text = text;
    [self createAttributedText];
}

- (void)setUser:(User *)user
{
    _user = user;
    [self createAttributedText];
}

- (void)setRetweeted_status:(Status *)retweeted_status
{
    _retweeted_status = retweeted_status;
    self.retweeted = NO;
    retweeted_status.retweeted = YES;
}

- (void)setRetweeted:(BOOL)retweeted
{
    _retweeted = retweeted;
    [self createAttributedText];
}

- (void)createAttributedText
{
    if (self.user == nil || self.text == nil) return;
    if (self.retweeted) {
        NSString *string = [NSString stringWithFormat:@"%@ : %@",self.user.name ,self.text];
        NSAttributedString *attributedString = [self attributedStringWithText:string];
        self.attributedText = attributedString;
    }else{
        self.attributedText = [self attributedStringWithText:self.text];
    }
}

- (NSAttributedString *)attributedStringWithText:(NSString *)text
{
    NSArray *regexResults = [self regexResultsWithText:text];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]init];
    [regexResults enumerateObjectsUsingBlock:^(RegexResult *result, NSUInteger idx, BOOL * _Nonnull stop) {
        if (result.isEmotion) {
            Emotion *emotion = [EmotionTool emotionWithDesc:result.string];
            EmotionAttachment *attach = [[EmotionAttachment alloc]init];
            attach.emotion = emotion;
            attach.bounds = CGRectMake(0, -3, StatusOriginalTextFont.lineHeight, StatusOriginalTextFont.lineHeight);
            NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
            [attributedString appendAttributedString:attachString];
        }else{
            NSMutableAttributedString *subStr = [[NSMutableAttributedString alloc]initWithString:result.string];
            
            //匹配#话题
            NSString *trendRegex = @"#[a-zA-Z0-9\\u4e00-\\u9fa5]+#";
            [result.string enumerateStringsMatchedByRegex:trendRegex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
                [subStr addAttribute:NSForegroundColorAttributeName value:Color(74, 102, 105) range:*capturedRanges];
                [subStr addAttribute:@"Link" value:*capturedStrings range:*capturedRanges];
            }];
            //匹配@提到
            NSString *mentionRegex = @"@[a-zA-Z0-9\\u4e00-\\u9fa5\\-]+ ?";
            [result.string enumerateStringsMatchedByRegex:mentionRegex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
                [subStr addAttribute:NSForegroundColorAttributeName value:Color(74, 102, 105) range:*capturedRanges];
                [subStr addAttribute:@"Link" value:*capturedStrings range:*capturedRanges];
            }];
            //匹配http网页
            NSString *httpRegex = @"http(s)?://([a-zA-Z|\\d]+\\.)+[a-zA-Z|\\d]+(/[a-zA-Z|\\d|\\-|\\+|_./?%&=]*)?";
            [result.string enumerateStringsMatchedByRegex:httpRegex usingBlock:^(NSInteger captureCount, NSString *const __unsafe_unretained *capturedStrings, const NSRange *capturedRanges, volatile BOOL *const stop) {
                [subStr addAttribute:NSForegroundColorAttributeName value:Color(74, 102, 105) range:*capturedRanges];
                [subStr addAttribute:@"Link" value:*capturedStrings range:*capturedRanges];
            }];
            [attributedString appendAttributedString:subStr];
        }
    }];
    [attributedString addAttribute:NSFontAttributeName value:StatusRichTextFont range:NSMakeRange(0, attributedString.length)];
    return  attributedString;
}













@end
